Laravel 5でDBのテスト
テスト用の環境変数
テスト実行時の環境変数はphpunit.xmlに設定されるtestingになる
.env.testingを設定しても良い
In addition, you may create a .env.testing file in the root of your project. This file will override the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.
phpunit.xmlに<env name="DB_CONNECTION" value="testing"/>のように設定しても良い
phpunit.xmlに定義したもののほうが.env.testingに定義したものより優先されるのでハマることがあるかも
テスト用DBのマイグレーション
前提
MySQLでCREATE DATABASE testing;をした
.env.testingでDBの接続設定を記述した
やり方
php artisan migrate:refresh --seed --env=testing
--env=testingをつけるとtesting環境に対して実行できる